package com.mazaiting.blog.controller

import com.mazaiting.blog.domain.Response
import com.mazaiting.blog.domain.User
import com.mazaiting.blog.service.UserService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 用户相关控制器
 */
@RestController
@RequestMapping("user")
class UserController {
    /**日志框架*/
    val logger = LoggerFactory.getLogger(javaClass)!!

    /**
     * 用户服务
     */
    @Autowired
    lateinit var service: UserService

    // 已使用
    /**
     * 登陆
     * @param user 用户信息
     * @return 返回成功与否，ret = 0 时成功，ret = -1 时失败
     */
    @PostMapping("/login")
    fun login(@RequestBody user: User): Response{
        val result = service.login(user)
        var ret = -1
        if (result === UserService.USER_VALID) ret = 0
        return Response(ret, result)
    }

    // 已使用
    /**
     * 注册
     * @param user 用户信息
     * @return 返回注册是否成功，ret = -1 失败；ret = 0 成功
     */
    @PostMapping("/register")
    fun register(@RequestBody user: User): Response {
        val result = service.register(user)
        var ret = -1
        if (result === UserService.USER_VALID) ret = 0
        return Response(ret, result)
    }
}